<?
global $AppUI;

if (!defined('DP_BASE_DIR')) {
	// TODO:
	// Redirect to a public HTML page in this case
	echo 'You shoud not access to this file directly!';
}
else {
	$type = $_REQUEST['type'];
	$id = $_REQUEST['id'];

	switch ($type) {
	case 'User':
		if (isset($id))
			getUserInfo($id);
		else {
			$AppUI->setMsg('Missing user ID', UI_MSG_ERROR);
			echo $AppUI->getMsg();
		}
		break;
	case 'Company':
		if (isset($id))
			getCompanyInfo($id);
		else {
			$AppUI->setMsg('Missing company ID', UI_MSG_ERROR);
			echo $AppUI->getMsg();
		}
		break;
	case 'DepartmentList':
		if (isset($id))
			getCompanyDepartment($id);
		else {
			$AppUI->setMsg('Missing company ID', UI_MSG_ERROR);
			echo $AppUI->getMsg();
		}
		break;
	case 'CostCenterList':
		if (isset($id))
			getCostCenter($id);
		else {
			$AppUI->setMsg('Missing cost center ID', UI_MSG_ERROR);
			echo $AppUI->getMsg();
		}
		break;
	case 'Department':
	default:
		$AppUI->setMsg('Function not ready', UI_MSG_WARNING);
		echo $AppUI->getMsg();
		break;
	}
}

function getUserInfo($user_id) {
	global $AppUI;

	$q = new DBQuery();
	$q->addTable('users', 'u');
	$q->addQuery('u.*');
	$q->addQuery('CONCAT(con.contact_first_name, " ", con.contact_last_name) AS contact_name');
	$q->addQuery('contact_email, company_id, company_name, dept_name, dept_id');
	$q->addJoin('contacts', 'con', 'user_contact = contact_id');
	$q->addJoin('companies', 'com', 'contact_company = company_id');
	$q->addJoin('departments', 'dep', 'dept_id = contact_department');
	$q->addWhere('u.user_id = '.$user_id);
	$user = $q->loadHash();
	if (!$user) {
		$AppUI->setMsg('Missing user ID', UI_MSG_ERROR);
		echo $AppUI->getMsg();
		return;
	}

	include 'modules/public/resources.info.php';
}

function getCompanyInfo($company_id) {
	global $AppUI;

	$q = new DBQuery();
	$q->addTable('companies', 'c');
	$q->addJoin('users', 'u', 'u.user_id = c.company_owner');
	$q->addJoin('contacts', 'co', 'u.user_contact = co.contact_id');
	$q->addQuery('c.*, CONCAT(contact_first_name, " ", contact_last_name) AS contact_name');
	$q->addWhere('c.company_id = '.$company_id);
	$company = $q->loadHash();
	if (!$company) {
		$AppUI->setMsg('Missing company ID', UI_MSG_ERROR);
		echo $AppUI->getMsg();
		return;
	}

	include 'modules/public/resources.info.php';
}

function getCompanyDepartment($company_id) {
	global $AppUI;

	$q = new DBQuery();
	$q->addTable('companies');
	$q->addQuery('company_name');
	$q->addWhere('company_id = '.$company_id);
	$company_name = $q->loadResult();

	if (!$company_name) {
		$AppUI->setMsg('Company not found', UI_MSG_ERROR);
		echo $AppUI->getMsg();
		return;
	}

	$q->addTable('departments');
	$q->addQuery('dept_id,dept_parent,dept_name');
	$q->addWhere('dept_company = '.$company_id);
	$q->addOrder('dept_parent, dept_name');
	$depts = $q->loadHashList('dept_id');

	if (!$depts) {
		$AppUI->setMsg('Company [ '.$company_name.' ] has no department', UI_MSG_WARNING);
		echo $AppUI->getMsg();
		return;
	}

	include 'modules/public/resources.info.php';
}
function getCostCenter($div_id) {
	global $AppUI;
	$div_name='';
	if($div_name !=0){
		$q = new DBQuery();
		$q->addTable('divisions');
		$q->addQuery('div_name');
		if($div_id != 0)
		$q->addWhere('div_id = '.$div_id);
		//echo $q->prepare();
		$div_name = $q->loadResult();
		if (!$div_name) {
			$AppUI->setMsg('Division not found', UI_MSG_ERROR);
			echo $AppUI->getMsg();
			return;
		}
	}

	$q = new DBQuery();
	$q->addTable('costcenters');
	$q->addJoin('divisions', null, 'div_id = cc_division');
	$q->addQuery('cc_id, cc_name, div_code');
	if($div_id != 0)
		$q->addWhere('cc_division = '.$div_id);
	$q->addOrder('cc_name');
	//echo $q->prepare();
	$costcenters = $q->loadHashList('cc_id');

	if (!$costcenters) {
		$AppUI->setMsg('Division [ '.$div_name.' ] has no cost center', UI_MSG_WARNING);
		echo $AppUI->getMsg();
		return;
	}

	include 'modules/public/resources.info.php';
}
?>
